<?php
namespace app\index\controller;

use think\Controller;
use think\Cache;
use think\Request;
use think\Session;



class Weixin extends Controller
{

    function __construct() {
        parent::__construct();
    }

    //获取access_token等信息
    public function weixin_auth(){
        $dao = new \app\model\Weixin();
        $wx_code = deletehtml($this->request->get('code'));
        $type = deletehtml($this->request->get('type'));
        if(empty($wx_code)){
            $wx_code = $dao->get_code();//获取微信网页授权code
        }




        $wx_code = deletehtml($this->request->get('code'));
        $dao = new \app\model\Weixin();
        $auth_info = $dao->auth_access_token($wx_code);//获取网页授权access_token
        $user_info = $dao->user_info($auth_info->access_token,$auth_info->openid);
        var_dump($user_info);die();
    }

    //自定义底部菜单创建
    public function create_menu(){
        $menu=[];
        $menu = array(
            [
                "name"=> "按钮1",
                "sub_button"=> array(
                    [
                        "type"=>"scancode_waitmsg",
                        "name"=> "按钮1-1",
                        "key"=> "rselfmenu_0_0",
                        "sub_button"=> [ ]
                    ],
                    [
                        "type"=> "scancode_push",
                        "name"=> "按钮1-2",
                        "key"=> "rselfmenu_0_1",
                        "sub_button"=> [ ]
                    ]
                )
            ],
            [
                "name"=> "按钮2",
                "sub_button"=> array(
                    [
                        "type"=>"scancode_waitmsg",
                        "name"=> "按钮2-1",
                        "key"=> "rselfmenu_0_0",
                        "sub_button"=> [ ]
                    ]
                )
            ],
            [
                "name"=> "按钮3",
                "sub_button"=> array(
                    [
                        "type"=>"scancode_waitmsg",
                        "name"=> "按钮3-1",
                        "key"=> "rselfmenu_0_0",
                        "sub_button"=> [ ]
                    ],[
                        "type"=>"scancode_waitmsg",
                        "name"=> "按钮3-2",
                        "key"=> "rselfmenu_0_0",
                        "sub_button"=> [ ]
                    ],[
                        "type"=>"scancode_waitmsg",
                        "name"=> "按钮3-3",
                        "key"=> "rselfmenu_0_0",
                        "sub_button"=> [ ]
                    ],[
                        "type"=>"scancode_waitmsg",
                        "name"=> "按钮3-4",
                        "key"=> "rselfmenu_0_0",
                        "sub_button"=> [ ]
                    ],[
                        "type"=>"scancode_waitmsg",
                        "name"=> "按钮3-5",
                        "key"=> "rselfmenu_0_0",
                        "sub_button"=> [ ]
                    ],[
                        "type"=>"scancode_waitmsg",
                        "name"=> "按钮3-6",
                        "key"=> "rselfmenu_0_0",
                        "sub_button"=> [ ]
                    ],
                )
            ]
        );
        $dao = new \app\model\Weixin();
        $menu = json($menu);
        $access_token = $dao->create_menu($menu,$this->access_token());//自定义底部菜单创建
        return json($access_token);
    }

    //获取微信网页授权code
    public function user_info(){
        $dao = new \app\model\Weixin();
        $wx_code = $dao->get_code();//获取微信网页授权code
//
        $wx_code = deletehtml($this->request->get('code'));
        $type = deletehtml($this->request->get('type'));
        if(empty($type))$type='snsapi_userinfo';
        if(!in_array($type,['snsapi_userinfo','snsapi_base']))$type='snsapi_userinfo';
        if(!empty($wx_code)){//存在code，说明是授权跳转的
            $dao = new \app\model\Weixin();
            $auth_info = $dao->auth_access_token($wx_code);//获取网页授权access_token
            $user_info = $dao->user_info($auth_info->access_token,$auth_info->openid);
            Session::set('user_info',$user_info);
        }else{
            $user_info = Session::get('user_info');
            if(!$user_info){
                //获取code
                $dao = new \app\model\Weixin();
                $wx_code = $dao->get_code();//获取微信网页授权code
            }
        }
    }
    public function user_code(){
        $wx_code = deletehtml($this->request->get('code'));
        $dao = new \app\model\Weixin();
        $auth_info = $dao->auth_access_token($wx_code);//获取网页授权access_token
        var_dump(2);
        var_dump($auth_info);
    }

    //获取微信access_token
    function access_token(){
        $dao = new \app\model\Weixin();
        $access_token = Cache::get('wx_access_token');
        if(!$access_token){
            $access_token = $dao->access_token();//获取微信access_token
        }
        return $access_token;
    }

}


